package linc.fun.common.rest.http.controller;

import linc.fun.common.rest.http.annotation.RestAsync;
import linc.fun.common.rest.http.request.JsonRestRequest;
import linc.fun.common.rest.http.response.JsonRestResponse;
import linc.fun.common.rest.http.service.JsonRestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;


/**
 * @author yqlin
 * @date 2022/5/5 14:53
 * @description
 */
@RestController
@RequestMapping("/api")
public class JsonRestController {
    private final JsonRestService service;

    @Qualifier("async-service")
    @Autowired
    private Executor executor;

    @Autowired
    public JsonRestController(JsonRestService jsonRestService) {
        this.service = jsonRestService;
    }

    @PostMapping("/rest")
    @RestAsync(async = false)
    public CompletableFuture<JsonRestResponse<?>> postRest(@RequestBody JsonRestRequest request) {
        return null;
    }

    @PostMapping("/rest2")
    public JsonRestResponse<?> postRest2(@RequestBody JsonRestRequest request) {
        return service.rest(request);
    }

    @PostMapping("/rest3")
    @RestAsync
    public CompletableFuture<JsonRestResponse<?>> postRest3(@RequestBody JsonRestRequest request) {
        return null;
    }

    @PostMapping("/rest4")
    public CompletableFuture<JsonRestResponse<?>> postRest4(@RequestBody JsonRestRequest request) {
        return CompletableFuture.supplyAsync(() -> service.rest(request), executor) ;
    }
}
